package com.jilian.mydemo.modul.my.ui;

import android.arch.lifecycle.Observer;
import android.arch.lifecycle.ViewModelProviders;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.baidu.mapapi.map.Text;
import com.jilian.mydemo.MyApplication;
import com.jilian.mydemo.R;
import com.jilian.mydemo.base.BaseActivity;
import com.jilian.mydemo.base.BaseDto;
import com.jilian.mydemo.modul.order.viewmodel.OrderViewModel;
import com.jilian.mydemo.utils.ToastUitl;

/**
 * 提现
 */
public class WithdrawalActivity extends BaseActivity {
    private EditText etMoney;
    private EditText etWechat;



    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        MyApplication.addActivity(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        MyApplication.removeActivity(this);
    }

    private OrderViewModel orderViewModel;
    private TextView tvOk;

    @Override
    protected void createViewModel() {
        orderViewModel = ViewModelProviders.of(this).get(OrderViewModel.class);
    }

    @Override
    public int intiLayout() {
        return R.layout.activity_withdrawal;
    }

    @Override
    public void initView() {
        setNormalTitle("提现", v -> finish());
        tvOk = (TextView) findViewById(R.id.tv_ok);
        etMoney = (EditText) findViewById(R.id.et_money);
        etWechat = (EditText) findViewById(R.id.et_wechat);

    }

    @Override
    public void initData() {

    }

    @Override
    public void initListener() {
        tvOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(TextUtils.isEmpty(etWechat.getText().toString())){
                    ToastUitl.showImageToastTips("请输入微信号");
                    return;
                }
                if(TextUtils.isEmpty(etMoney.getText().toString())){
                    ToastUitl.showImageToastTips("请输入金额");
                    return;
                }
                showLoadingDialog();
                orderViewModel.withdraw(etMoney.getText().toString(),etWechat.getText().toString());
                orderViewModel.getWithdrliveData().observe(WithdrawalActivity.this, new Observer<BaseDto>() {
                    @Override
                    public void onChanged(@Nullable BaseDto baseDto) {
                        hideLoadingDialog();
                        if (baseDto.isSuccess()) {
                            startActivity(new Intent(WithdrawalActivity.this, WithdrawalSuccessActivity.class));
                            finish();
                        } else {
                            ToastUitl.showImageToastTips(baseDto.getMsg());
                        }
                    }
                });

            }
        });
    }
}
